#pragma once
#include <Queue>
#include <memory>
#include <mutex>
#include <thread>
#include <map>
#include <unordered_map>
#include <functional>
#include <json/json.h>
#include <json/reader.h>

#include "CSession.h"
#include "MsgNode.h"
#include "Singleton.h"
#include "global.h"
#include "data.h"
using namespace std;


typedef function<void(shared_ptr<CSession>, const short &msg_id,
                      const string &msg_data)>
    FunCallBack;
class LogicSystem : public Singleton<LogicSystem> {
  friend class Singleton<LogicSystem>;

 public:
  ~LogicSystem();
  void PostMsgToQue(shared_ptr<LogicNode> msg);

 private:
  LogicSystem();
  void DealMsg();
  void RegisterCallBacks();
  void LoginHandler(shared_ptr<CSession>, const short &msg_id,
                    const string &msg_data);

 private:
  bool _b_stop;
  std::mutex _mutex;
  std::thread _worker_thread;
  std::condition_variable _consume;
  std::queue<shared_ptr<LogicNode>> _msg_que;

  std::map<uint16_t, FunCallBack> _fun_callbacks;
  std::unordered_map<int, std::shared_ptr<UserInfo>> _users;
};
